﻿/**
 * ...
 * @author Артём Григорьев
 */

package tab;

import flash.Lib;
import gui.hug.HUGSprite;
import gui.hug.HUGTabbedPanel;
import gui.hug.Tab;

import list.NewList;
import list.OpenTabEvent;

import loader.Loader;
import client.Channel;

class ChannelsTab extends Tab
{
	private static inline var 	TITLE = "Список каналов";
	
	private var loader   : 	Loader;
	private var newsList : 	NewList;
	
	public function new(panel : HUGTabbedPanel = null, uid : Int) 
	{
		// initialization
		newsList = new NewList(4, 7, 600);
		Loader.createInstance();
		loader = Loader.getInstance();
		
		// creating layout
		var sprite : HUGSprite = new HUGSprite();
		sprite.addChild(newsList);
		
		// loading data from server
		loader.getChannels(onLoad);
		
		// listen for opening new tabs
		var onOpenTabEvent = function(e : OpenTabEvent) {
			var channel : Channel = cast(e.getContent(), Channel);
			var tab = new ItemsTab(channel.getChanId(), channel.getName(), panel, uid);
			tab.setId(channel.getChanId());
			panel.addTab(tab); 
		};
		newsList.addEventListener(OpenTabEvent.OPEN_TAB, onOpenTabEvent);
		
		super(sprite, TITLE,0, panel, true);
	}

	
	public function onLoad(data : Array<Channel>) : Void
	{
		for (channel in data)
		{   
			newsList.addItem(channel,0);
		}
	}	
	
}